import Qs from 'qs'
import axios from 'axios'
import Common from './common'

var Release = false;

var redirect_url = 'https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=jsjffwzq';
// var api_url = 'http://111.1.29.66:12000/v1/';
// var api_url = 'https://visit-api.jx-ding.com/v1/';
var api_url = 'https://js-shzl-api.jx-ding.com.cn/v1/';
// var api_url = 'https://visit-api-test.jx-ding.com/v1/';
// var api_url = 'http://192.168.1.103:19000/v1/'
// var api_url = 'http://localhost:19000/v1/';


export default {
    get(options, callback, failCallback) {
        let apiServer = this.getRequestURL(options)
        axios.get(apiServer).then(result => {
            let data = result.data
            // 验证token失败
            if (data.status === 4004) {
                Common.showToast('登录信息过期', 'warn');
                Common.setLocal('token', '')
                window.location.href = redirect_url;
                this.that.$store.dispatch('loginOut')
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else if (data.status !== 200) {
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else {
                (typeof (callback) === 'function') && callback(data.data)
            }
        }).catch(error => {
            console.log(error)
            (typeof (failCallback) === 'function') && failCallback(error)
        })
    },
    call(options, callback, failCallback) {
        options = options || {};
        options.data = options.data ? options.data : {}
        for (let key in options.data) {
            if (options.data[key] === null || options.data[key] === undefined) {
                delete options.data[key]
            }
        }
        let apiServer = this.getRequestURL(options)

        let data = this.getData(options.data)
        axios.post(apiServer, Qs.stringify(data), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(result => {

            let data = result.data;
            // 验证token失败
            if (data.status === 4004) {
                Common.showToast('登录信息过期', 'warn')
                Common.setLocal('token', '')
                // window.location.href = 'http://localhost:19000/renshe/go'
                // window.location.href = 'http://renshe-api.growhappy.cn/renshe/go';
                window.location.href = redirect_url;
                this.that.$store.dispatch('loginOut')
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else if (data.status !== 200) {
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else {
                (typeof (callback) === 'function') && callback(data.data)
            }
        }).catch(error => {
            (typeof (failCallback) === 'function') && failCallback(error)
        })
    },
    callForm(options, callback, failCallback) {
        let apiServer = this.getRequestURL(options)
        let data = options.data
        data.append('token', Common.getLocal('token'))
        axios.post(apiServer, data, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(result => {
            let data = result.data;
            // 验证token失败
            if (data.status === 4004) {
                Common.showToast('登录信息过期', 'warn')
                Common.setLocal('token', '')
                // window.location.href = 'http://localhost:19000/renshe/go'
                window.location.href = redirect_url;
                this.that.$store.dispatch('loginOut')
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else if (data.status !== 200) {
                (typeof (failCallback) === 'function') && failCallback(data.message)
            } else {
                (typeof (callback) === 'function') && callback(data.data)
            }
        }).catch(error => {
            console.log(error);
            (typeof (failCallback) === 'function') && failCallback(error);
        })
    },

    concurrentCall(callback, failCallback, ...options) {
        let calls = []
        for (let op of options) {
            let apiServer = this.getRequestURL(op)
            let data = this.getData(op.data)
            // console.log(apiServer)
            // console.log(data)
            calls.push(axios.post(apiServer, Qs.stringify(data)))
        }
        axios.all(calls)
            .then(axios.spread((...res) => {
                let results = []
                for (let result of res) {
                    // console.log(result.data)
                    if (result.data.status === 4004) {
                        Common.showToast('登录信息过期', 'warn')
                        Common.setLocal('token', '')
                        // window.location.href = 'http://localhost:19000/renshe/go'
                        window.location.href = redirect_url;
                        this.that.$store.dispatch('loginOut')
                        return
                    } else if (result.data.status !== 200) {
                        (typeof (failCallback) === 'function') && failCallback(result.data)
                        return
                    }
                    results.push(result.data)
                }
                (typeof (callback) === 'function') && callback(...results)
            })).catch((error) => {
            (typeof (failCallback) === 'function') && failCallback(error)
        })

    },


    /**
     * 获取请求的url
     */
    getRequestURL(options) {
        let API = api_url;
        let param = options.param
        let url = options.url || API

        let service = options.service || 'Default.index' // 控制器

        // 拼接url
        let apiServer = url + service
        let name
        if (options.param) {
            for (name in param) {
                if (param[name]) {
                    apiServer += name + '=' + param[name] + '&'
                }
            }
            // apiServer += 'token=' + Common.getLocal('token')
        }
        return apiServer.replace(/&$/gi, '')
    },

    /**
     * 获取数据
     * @param  object data 待提交数据
     */
    getData(data) {
        // data['cors'] = true
        let token = Common.getLocal('token')
        data['token'] = token || ''
        // data['client'] = 'weixin'
        // data['sign'] = this.sign(data)
        return data
    }
}
